package com.powernode.dataservice.service;

import com.powernode.api.model.RechargeRecord;
import com.powernode.api.service.RechargeRecordService;
import com.powernode.constants.YLBConstants;
import com.powernode.dataservice.mapper.FinanceAccountMapper;
import com.powernode.dataservice.mapper.RechargeRecordMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@DubboService(interfaceClass = RechargeRecordService.class,version = "1.0.0")
public class RechargeRecordServiceImpl implements RechargeRecordService {

    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    private FinanceAccountMapper financeAccountMapper;

    /**
     * 添加充值订单
     * @param rechargeRecord
     */
    @Override
    public void addRecharge(RechargeRecord rechargeRecord) {
        rechargeRecordMapper.insertSelective(rechargeRecord);
    }

    /**
     * 根据 快钱返回的结果 处理  充值业务  修改 订单  修改  账户
     * @param orderId
     * @param payResult
     * @param payAmount
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int dealWithNotify(String orderId, String payResult, String payAmount) {
        /*默认  没有处理订单*/
        int result = 0;
        /*查询 订单  判断订单 状态  充值中*/
        RechargeRecord rechargeRecord = rechargeRecordMapper.selectByRechargeNo(orderId);
        if (rechargeRecord!=null){
            /*判断状态*/
            if (rechargeRecord.getRechargeStatus()== YLBConstants.RECHARGE_PROCESSING){
                /*订单还未处理*/
                /* 将 元 转化为 分*/
                String fen =
                        rechargeRecord.getRechargeMoney()
                                .multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();

                /*比较 数据的 金额  和 快钱返回的 金额  是否 一致*/
                if (fen.equals(payAmount)){
                    /*判断  支付结果*/
                    if ("10".equals(payResult)){
                        /*支付成功*/
                        /*1 更新 充值状态*/
                        int rows = rechargeRecordMapper.updateStatus(rechargeRecord.getId(),YLBConstants.RECHARGE_SUCCESS);
                        if (rows<1){
                            throw new RuntimeException("充值状态修改异常");
                        }
                        /*2 更新 账户*/
                        rows = financeAccountMapper.updateMoney(rechargeRecord.getUid(),rechargeRecord.getRechargeMoney());
                        if (rows<1){
                            throw new RuntimeException("账户更新资金异常");
                        }
                        result = 4;
                    }else {
                        /*支付失败*/
                        /*1 更新 充值状态*/
                        int rows = rechargeRecordMapper.updateStatus(rechargeRecord.getId(),YLBConstants.RECHARGE_FAIL);
                        if (rows<1){
                            throw new RuntimeException("充值状态修改异常");
                        }

                        result = 5;
                    }
                }else {
                    /*金额不一致*/
                    result = 3;
                }
            }else {
                /*订单出了过了*/
                result=2;
            }
        }else {
            /*订单不存在*/
            result = 1;
        }
        return result;
    }
}
